package com.hooper.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hooper.common.PageResult;
import com.hooper.mapper.SysRoleMapper;
import com.hooper.model.system.SysRole;
import com.hooper.model.vo.RoleInsertVo;
import com.hooper.model.vo.SysRoleQueryVo;
import com.hooper.service.SysRoleService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author Mind
 * @description 针对表【sys_role(角色)】的数据库操作Service实现
 * @createDate 2023-03-28 14:40:01
 */
@Service
public class SysRoleServiceImpl implements SysRoleService {

    @Resource
    private SysRoleMapper sysRoleMapper;

    @Override
    public List<SysRole> getAllRoles() {
        return sysRoleMapper.selectAll();
    }

    @Override
    public PageResult<SysRole> pageByConditions(int currentPage, int pageSize, SysRoleQueryVo queryVo) {
        PageHelper.startPage(currentPage, pageSize);
        List<SysRole> roleList = sysRoleMapper.selectAllByVo(queryVo);
        PageInfo<SysRole> pageInfo = new PageInfo<>(roleList);
        PageResult<SysRole> pageResult = new PageResult<>();
        pageResult.setCurrentPage(currentPage);
        pageResult.setPageSize(pageSize);
        pageResult.setData(pageInfo.getList());
        pageResult.setTotal((int) pageInfo.getTotal());
        return pageResult;
    }

    @Override
    public void saveRole(RoleInsertVo role) {
        SysRole dbRole = new SysRole();
        BeanUtils.copyProperties(role, dbRole);
        sysRoleMapper.insert(dbRole);
    }

    @Override
    public void updateRoleById(SysRole newRole) {
        newRole.setUpdateTime(new Date());
        sysRoleMapper.updateById(newRole);
    }

    @Override
    public void delRoleById(Long id) {
        sysRoleMapper.deleteById(id);
    }

    @Override
    public void delBatchByIds(List<Long> ids) {
        sysRoleMapper.deleteBatchByIds(ids);
    }

    @Override
    public SysRole getById(Long id) {
        return sysRoleMapper.selectById(id);
    }

    @Override
    public void saveBatch(List<RoleInsertVo> list) {
        sysRoleMapper.saveBatch(list);
    }
}




